package cn.cxion.chat.app.view.popupview;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Switch;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.lqr.optionitemview.OptionItemView;
import com.lxj.xpopup.core.BottomPopupView;
import com.lxj.xpopup.util.XPopupUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.cxion.chat.R;
import cn.cxion.chat.app.bean.AppointmentLocale;
import cn.cxion.chat.app.bean.MessageEvent;
import cn.cxion.chat.app.third.location.ui.activity.MyLocationActivity;
import cn.cxion.chat.app.util.UiUtils;
import cn.cxion.chat.kit.WfcBaseActivity;

public class AppointmentLocalePopupView extends BottomPopupView {


    @Bind(R.id.cancle)
    TextView cancle;
    @Bind(R.id.ok)
    TextView ok;
    @Bind(R.id.op_surelocale)
    OptionItemView opSurelocale;
    @Bind(R.id.switch_surelocale)
    Switch switchSurelocale;
    @Bind(R.id.ll_surelocale)
    LinearLayout llSurelocale;
    @Bind(R.id.op_suretime)
    OptionItemView opSuretime;
    @Bind(R.id.switch_appointlawfirm)
    Switch switchAppointlawfirm;
    @Bind(R.id.ll_appointlawfirm)
    LinearLayout llAppointlawfirm;
    @Bind(R.id.selectlocale)
    OptionItemView selectlocale;
    Activity activity;
    AppointmentLocale appointmentLocale;
    OnLinstener linstener;
    private String localeName="";

    public AppointmentLocalePopupView(@NonNull Context context) {
        super(context);
        activity = (Activity) context;
    }
    public void setLinstener(OnLinstener linstener){
        this.linstener=linstener;
    }


    public AppointmentLocalePopupView(@NonNull Context context, AppointmentLocale appointmentLocale) {
        super(context);
        activity = (Activity) context;
        if (appointmentLocale != null)
            this.appointmentLocale = appointmentLocale;
        else
            this.appointmentLocale = new AppointmentLocale();
    }


    @Override
    protected int getImplLayoutId() {
        return R.layout.popup_appointmentlocale;
    }

    @Override
    protected void onCreate() {
        super.onCreate();
        initView();
    }

    //初始化界面
    private void initView() {
        ButterKnife.bind(this);
        EventBus.getDefault().register(this);
        switchAppointlawfirm.setChecked(appointmentLocale.isAppointmentLawFirm());
        switchSurelocale.setChecked(appointmentLocale.isSureLocale());
        UiUtils.setRightText(appointmentLocale.getLocaleName(),selectlocale);
    }


    @Override
    protected void onShow() {
        super.onShow();


    }

    @Override
    protected void onDismiss() {
        super.onDismiss();
    }

    @Override
    protected int getMaxHeight() {
        return (int) (XPopupUtils.getWindowHeight(getContext()) * 0.75);
    }


    @OnClick({R.id.cancle, R.id.ok, R.id.op_surelocale, R.id.switch_surelocale, R.id.ll_surelocale, R.id.op_suretime, R.id.switch_appointlawfirm, R.id.ll_appointlawfirm, R.id.selectlocale})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.cancle:
                dismiss();
                break;
            case R.id.ok:
                appointmentLocale.setSureLocale(switchSurelocale.isChecked());
                appointmentLocale.setAppointmentLawFirm(switchAppointlawfirm.isChecked());
                appointmentLocale.setLocaleName(localeName);
                linstener.ok(appointmentLocale);
                dismiss();
                break;
            case R.id.op_surelocale:
                changeSureLocale();
                break;
            case R.id.ll_surelocale:
                changeSureLocale();
                break;
            case R.id.op_suretime:
                changeLawFirm();
                break;
            case R.id.ll_appointlawfirm:
                changeLawFirm();
                break;
            case R.id.selectlocale:
                showLocation();
                break;
        }
    }
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void getMessage(MessageEvent messageEvent) {
        if(messageEvent.getKey()==0){
            localeName=messageEvent.getMessage();
            selectlocale.setRightText(messageEvent.getMessage());
        }
    }
    private void changeSureLocale() {
        if (switchSurelocale.isChecked()) {
            switchSurelocale.setChecked(false);
        } else {
            switchSurelocale.setChecked(true);
        }
    }

    private void changeLawFirm() {
        if (switchAppointlawfirm.isChecked()) {
            switchAppointlawfirm.setChecked(false);
        } else {
            switchAppointlawfirm.setChecked(true);
        }
    }

    private void showLocation() {
        Intent intent = new Intent(getContext(), MyLocationActivity.class);
        activity.startActivityForResult(intent, 100);
    }

    public interface OnLinstener{
        void ok(AppointmentLocale appointmentLocale);
    }

}
